package com.dai.yygh.controller;

import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dai.yygh.common.result.Result;
import com.dai.yygh.common.util.MD5;
import com.dai.yygh.model.hosp.HospitalSet;
import com.dai.yygh.service.HospitalSetService;
import com.dai.yygh.vo.hosp.HospitalQueryVo;
import com.dai.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(tags="医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation(value ="获取所有医院设置信息")
    @GetMapping("findAll")
    public Result findAllHospitalSet(){
        List<HospitalSet> list=hospitalSetService.findAll();
        return Result.ok(list);
    }

    @ApiOperation(value = "逻辑删除医院设置")
    @DeleteMapping("{id}")
    public Result removeHospSet(@PathVariable Long id){
        boolean flag=hospitalSetService.deleteremoveHospSetById(id);
        if(flag) return Result.ok();
        else return Result.fail();
    }

    //条件查询分页
    @ApiOperation(value = "分页查询医院设置信息")
    @PostMapping("findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(@PathVariable long current, @PathVariable long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        //创建page对象，传递当前页，每页记录数
        Page<HospitalSet> page = new Page<>(current, limit);
        if(StringUtils.isEmpty(hospitalSetQueryVo.getHosname())||StringUtils.isEmpty(hospitalSetQueryVo.getHoscode())){
            List<HospitalSet> list=hospitalSetService.findAll();
            return Result.ok(list);
        }
        List<HospitalSet> list=hospitalSetService.findPageHospSet(page,hospitalSetQueryVo);
        return Result.ok(list);
    }

    //添加医院设置
    @ApiOperation(value = "添加医院设置信息")
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        hospitalSet.setStatus(1);
        Random random=new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        boolean flag=hospitalSetService.saveHospitalSet(hospitalSet);
        if(flag) return Result.ok("添加成功");
        else return Result.ok("添加失败");
    }

    //根据id获取医院设置
    @ApiOperation(value = "根据id获取医院设置信息")
    @GetMapping("getHospSet/{id}")
    public Result getHospSet(@PathVariable Long id){
        HospitalSet hospitalSet=hospitalSetService.getHospSet(id);
        if(hospitalSet!=null) return Result.ok(hospitalSet);
        else return Result.fail("未查询到对象");
    }

    //修改医院设置
    @ApiOperation(value = "修改医院设置")
    @PostMapping("updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean flag=hospitalSetService.updateHospitalSet(hospitalSet);
        if(flag) return Result.ok("修改成功");
        else return Result.fail("修改失败");
    }

    //批量删除医院设置
    @ApiOperation(value = "批量删除医院设置")
    @DeleteMapping("batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idlist){
       Boolean flag= hospitalSetService.batchRemoveHospitalSet(idlist);
       if(flag) return Result.ok();
       else return Result.fail();
    }

    //医院设置锁定和解锁
    @ApiOperation(value = "医院的锁定设置")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status){
        HospitalSet hospitalSet= hospitalSetService.getHospSet(id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateHospitalSet(hospitalSet);
        return Result.ok();
    }




    //发送签名的密钥
    @ApiOperation(value = "发送签名的密钥")
    @PutMapping("sendKey/{id}")
    public Result sendHospKey(@PathVariable Long id){
        HospitalSet hospSet = hospitalSetService.getHospSet(id);
        String signKey = hospSet.getSignKey();
        String hoscode = hospSet.getHoscode();
        //TOOOO


        return Result.ok();

    }

}
